package site.yunnong.atvris.common.enumeration;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public enum ProvinceEnum {
    BEI_JING("110000", "北京市", "11"),
    TIAN_JIN("120000", "天津市", "12"),
    HE_BEI("130000", "河北省", "13"),
    SAN_XI("140000", "山西省", "14"),
    NEI_MENG("150000", "内蒙古自治区", "15"),
    LIAO_NING("210000", "辽宁省", "21"),
    JI_LIN("220000", "吉林省", "22"),
    HEI_LONG_JIANG("230000", "黑龙江省", "23"),
    SHANG_HAI("310000", "上海市", "31"),
    JIANG_SU("320000", "江苏省", "32"),
    ZHE_JIANG("330000", "浙江省", "33"),
    AN_HUI("340000", "安徽省", "34"),
    FU_JIAN("350000", "福建省", "35"),
    JIANG_XI("360000", "江西省", "36"),
    SHAN_DONG("370000", "山东省", "37"),
    HE_NAN("410000", "河南省", "41"),
    HU_BEI("420000", "湖北省", "42"),
    HU_NAN("430000", "湖南省", "43"),
    GUANG_DONG("440000", "广东省", "44"),
    GUANG_XI("450000", "广西壮族自治区", "45"),
    HAI_NAN("460000", "海南省", "46"),
    CHONG_QING("500000", "重庆市", "50"),
    SI_CHUAN("510000", "四川省", "51"),
    GUI_ZHOU("520000", "贵州省", "52"),
    YUN_NAN("530000", "云南省", "53"),
    XI_ZANG("540000", "西藏自治区", "54"),
    SHAN_XI("610000", "陕西省", "61"),
    GAN_SU("620000", "甘肃省", "62"),
    QING_HAI("630000", "青海省", "63"),
    NING_XIA("640000", "宁夏回族自治区", "64"),
    XIN_JIANG("650000", "新疆维吾尔自治区", "65"),
    TAI_WAN("710000", "台湾省", "71"),
    XIANG_GANG("810000", "香港特别行政区", "81"),
    AO_MEN("820000", "澳门特别行政区", "82"),
    XU_NI("666666", "虚拟省", "666666");

    public final String code;
    public final String name;
    public final String prefix;
    private static final Map<String, ProvinceEnum> CODE_MAP = (Map)Arrays.stream(values()).collect(Collectors.toMap((e) -> {
        return e.code;
    }, (e) -> {
        return e;
    }));
    private static final Map<String, ProvinceEnum> PREFIX_MAP = (Map)Arrays.stream(values()).collect(Collectors.toMap((e) -> {
        return e.prefix;
    }, (e) -> {
        return e;
    }));

    private ProvinceEnum(String code, String name, String prefix) {
        this.code = code;
        this.name = name;
        this.prefix = prefix;
    }

    public static ProvinceEnum fromCode(String provinceCode) {
        return (ProvinceEnum)CODE_MAP.get(provinceCode);
    }

    public static ProvinceEnum fromPrefix(String prefix) {
        return (ProvinceEnum)PREFIX_MAP.get(prefix);
    }

    public static ProvinceEnum fromCityCode(String cityCode) {
        if (!Objects.isNull(cityCode) && !"".equals(cityCode.trim())) {
            Optional<ProvinceEnum> optional = Arrays.stream(values()).filter((e) -> {
                return cityCode.startsWith(e.prefix);
            }).findFirst();
            return optional.isPresent() ? (ProvinceEnum)optional.get() : null;
        } else {
            return null;
        }
    }
}
